package byggeTegning.geometry;

import javax.media.j3d.*;
import javax.vecmath.*;

public class TriangleSet extends Shape3D
{
//  private static final float width=1.0f;
//  private static final float height=1.0f;
//
//  private static final float d=width/8; // /4;
//  
//  // Point3f p=new Point3f(0.0f,0.0f,0.0f);
//  private static final float px=0.0f;
//  private static final float py=0.0f;
//  private static final float pz=0.0f;
//
//  // Point3f q=new Point3f(0.0f,0.0f,1.0f);
//  private static final float qx=0.0f;
//  private static final float qy=0.0f;
//  private static final float qz=1.0f;
//    
//    // Back Rectangle
//  private static final Point3f p01=new Point3f(px,py+d,pz);
//  private static final Point3f p02=new Point3f(px+d,py,pz);
//  private static final Point3f p11=new Point3f(px+width-d,py,pz);
//  private static final Point3f p12=new Point3f(px+width,py+d,pz);
//  private static final Point3f p21=new Point3f(px+width,py+height-d,pz);
//  private static final Point3f p22=new Point3f(px+width-d,py+height,pz);
//  private static final Point3f p31=new Point3f(px+d,py+height,pz);
//  private static final Point3f p32=new Point3f(px,py+height-d,pz);
//    // Front Rectangle
//  private static final Point3f q01=new Point3f(qx,qy+d,qz);
//  private static final Point3f q02=new Point3f(qx+d,qy,qz);
//  private static final Point3f q11=new Point3f(qx+width-d,qy,qz);
//  private static final Point3f q12=new Point3f(qx+width,qy+d,qz);
//  private static final Point3f q21=new Point3f(qx+width,qy+height-d,qz);
//  private static final Point3f q22=new Point3f(qx+width-d,qy+height,qz);
//  private static final Point3f q31=new Point3f(qx+d,qy+height,qz);
//  private static final Point3f q32=new Point3f(qx,qy+height-d,qz);
//
//  // Center in Back Face
//  private static final Point3f pc=new Point3f(p01.x+(p12.x-p01.x)/2,
//                           p01.y+(p32.y-p01.y)/2,
//                           p01.z);  
//    
//  // Center in Front Face
//  private static final Point3f qc=new Point3f(q01.x+(q12.x-q01.x)/2,
//                           q01.y+(q32.y-q01.y)/2,
//                           q01.z);  
//  
  private static final Point2f texCoord[]
          = { new Point2f(0.0f, 0.0f),
              new Point2f(0.5f, 0.0f),
              new Point2f(0.5f, 0.5f),
            };
//    
//  private static final Point3f[] verts // Forside: punktene nevnes 'mot klokka'
//          = {
//  //              q0, q1, q2, q3,  // front face - triangle 1
//              q01, qc, q32, // front face - triangle 1
//              q32, qc, q31, // front face - triangle 2
//              q31, qc, q22, // front face - triangle 3
//              q22, qc, q21, // front face - triangle 4
//              q21, qc, q12, // front face - triangle 5
//              q12, qc, q11, // front face - triangle 6
//              q11, qc, q02, // front face - triangle 7
//              q02, qc, q01, // front face - triangle 8
//            
//              p32, q32, q31, //p31,     // top face left - triangle 1
//                        q31, p31, p32,  // top face left - triangle 2
//                
//              p31, q31, q22, //p22,     // top face top - triangle 1
//                        q22, p22, p31,  // top face top - triangle 2
//                
//              p22, q22, q21, //p21,     // top face right - triangle 1
//                        q21, p21, p22,  // top face right - triangle 2
//                
//              q01, q32, p32, //p01,     // left, side face - triangle 1
//                        p32, p01, q01,  // left, side face - triangle 2
//                
//              p12, p21, q21, //q12,     // right, side face - triangle 1
//                        q21, q12, p12,  // right, side face - triangle 2
//                
//              p11, p12, q12, //q11,     // botum face right - triangle 1
//                        q12, q11, p11,  // botum face right - triangle 2
//                
//              p02, p11, q11, //q02,     // botum face botum - triangle 1
//                        q11, q02, p02,  // botum face botum - triangle 2
//                
//              p01, p02, q02, //q01,     // botum face - triangle 1
//                        q02, q01, p01,  // botum face - triangle 2
//                
//  //              p0, p3, p2, p1   // back face
//              p01, pc, p02, // back face - triangle 1
//              p02, pc, p11, // back face - triangle 2
//              p11, pc, p12, // back face - triangle 3
//              p12, pc, p21, // back face - triangle 4
//              p21, pc, p22, // back face - triangle 5
//              p22, pc, p31, // back face - triangle 6
//              p31, pc, p32, // back face - triangle 7
//              p32, pc, p01, // back face - triangle 8
//            };
    

  public TriangleSet(Point3f[] verts)
  { int i,face;

    TriangleArray tetra = new TriangleArray(48+2*8*3, TriangleArray.COORDINATES |
                   TriangleArray.NORMALS | TriangleArray.TEXTURE_COORDINATE_2);

    tetra.setCoordinates(0, verts);
    for(i=0;i<48+2*8*3;i++)
    {
      tetra.setTextureCoordinate(i,texCoord[i%3]);
    }

    Vector3f normal = new Vector3f();
    Vector3f v1 = new Vector3f();
    Vector3f v2 = new Vector3f();

    Point3f[] pts=new Point3f[3];
    for(i=0;i<3;i++) pts[i]=new Point3f();
    for(face=0;face<16+2*8;face++)
    { tetra.getCoordinates(face*3,pts);
      v1.sub(pts[1],pts[0]);
      v2.sub(pts[2],pts[0]);
      normal.cross(v1,v2);
      normal.normalize();
      for(i=0;i<3;i++)
      {
        tetra.setNormal((face*3 + i),normal);
      }
    }
    
    this.setGeometry(tetra);
    this.setAppearance(new Appearance());
  }
}